package com.fks.fks_service.utils;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fks.fks_service.pojo.ProductConfiguration;

import java.util.ArrayList;
import java.util.List;

public class JsonUtils {


    public List<ProductConfiguration> titleAndIsSelected(String jsonStr){
        // 创建 ObjectMapper 对象
        ObjectMapper objectMapper = new ObjectMapper();
        List<ProductConfiguration> list = new ArrayList<>();
        int times = 0;
        try {
            // 解析 JSON 字符串为 JsonNode
            JsonNode jsonNode = objectMapper.readTree(jsonStr);

            // 检查 JsonNode 是否是数组
            if (jsonNode.isArray()) {
                // 遍历数组中的每个元素
                for (JsonNode item : jsonNode) {
                    String title = item.get("title").asText();
                    boolean isSelected = item.get("isSelected").asBoolean();
                    boolean isDisplayed = item.get("isDisplayed").asBoolean();
                    ProductConfiguration productConfiguration = new ProductConfiguration();
                    productConfiguration.setTitle(title);
                    productConfiguration.setSelected(isSelected);
                    productConfiguration.setDisplayed(isDisplayed);
                    list.add(productConfiguration);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }


    public List<ProductConfiguration> titleAndIsSelectedAndPrice(String jsonStr){
        // 创建 ObjectMapper 对象
        ObjectMapper objectMapper = new ObjectMapper();
        List<ProductConfiguration> list = new ArrayList<>();
        int times = 0;
        try {
            // 解析 JSON 字符串为 JsonNode
            JsonNode jsonNode = objectMapper.readTree(jsonStr);

            // 检查 JsonNode 是否是数组
            if (jsonNode.isArray()) {
                // 遍历数组中的每个元素
                for (JsonNode item : jsonNode) {
                    String title = item.get("title").asText();
                    String price = item.get("price").asText();
                    boolean isSelected = item.get("isSelected").asBoolean();
                    boolean isDisplayed = item.get("isDisplayed").asBoolean();
                    ProductConfiguration productConfiguration = new ProductConfiguration();
                    productConfiguration.setTitle(title);
                    productConfiguration.setPrice(price);
                    productConfiguration.setSelected(isSelected);
                    productConfiguration.setDisplayed(isDisplayed);
                    list.add(productConfiguration);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }



}
